//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Blogs.Controls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for CSBlogPage.
	/// </summary>
	public class CSBlogPage : CommunityServer.Components.CSFormPage
	{
		public CSBlogPage()
		{
		  WeblogConfig = WeblogConfiguration.Instance();
		}

        private WeblogConfiguration  WeblogConfig;
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad (e);
            string appKey = CSContext.Current.ApplicationKey;
            if(appKey != null)
            {
                Weblog blog = Weblogs.GetWeblog(appKey,true,true);

                if(blog != null)
                {
                    string theme = blog.Theme;
                    ThemeConfig tc = ThemeManager.Instance().GetTheme(theme);

                    SetStyleSheets(blog, tc);
                    SetAutoSyndication(blog);
                    SetMetaTags(blog);
                }
            }
        }


        protected virtual void SetStyleSheets(Weblog blog, ThemeConfig tc)
        {
            if(tc.EnableCSS)
            {
                Head.AddStyle(CSSPath(blog.Theme,"style.css"),"screen",Context);
            }

            if(tc.EnablePrintCSS)
            {
                Head.AddStyle(CSSPath(blog.Theme,"print.css"),"print",Context);
            }

            if(tc.EnableCSS && blog.SecondaryCSS != null)
                Head.AddStyle(CSSPath(blog.Theme, blog.SecondaryCSS),"screen",Context);

            if(WeblogConfig.EnableThemes && blog.CSSOverride != null)
                Head.AddStyle(BlogUrls.Instance().CustomCSS(blog.ApplicationKey),"screen",Context, StyleRelativePosition.Last);
        }

        protected string CSSPath(string theme, string fileName)
        {
            if(theme == null || !WeblogConfig.EnableThemes)
                theme = WeblogConfig.DefaultTheme;

            string path = string.Format("{0}{1}/style/{2}",WeblogConfig.ThemeLocation,theme,fileName);
            return this.ResolveUrl(path);
        }
        

        protected virtual void SetAutoSyndication(Weblog blog)
        {
            CSContext cntx = CSContext.Current;
            BlogUrls bu = BlogUrls.Instance();

            string title = Globals.HtmlEncode(blog.Name);

            if(blog.EnableRss)
            {
                if (!Globals.IsNullorEmpty(blog.ExternalFeedUrl))
                {
                    Head.AddRssAutoDiscovery(title,blog.ExternalFeedUrl,Context);
                }
                else
                {
					if (Permissions.ValidatePermissions(blog, Permission.View, Users.GetAnonymousUser(true)))
						Head.AddRssAutoDiscovery(title, Globals.FullPath(bu.Rss(cntx.ApplicationKey)), Context);
					else if (cntx.SiteSettings.EnableSecureSyndication)
						Head.AddRssAutoDiscovery(title, Globals.FullPath(SiteUrls.FormatUrlWithUserNameToken(bu.Rss(cntx.ApplicationKey), cntx.User, false)), Context);
				}
            }

            if(blog.EnableAtom)
            {
                if (!Globals.IsNullorEmpty(blog.ExternalFeedUrl))
                {
                    Head.AddRssAutoDiscovery(title,blog.ExternalFeedUrl,Context);
                }
                else
                {
					if (Permissions.ValidatePermissions(blog, Permission.View, Users.GetAnonymousUser(true)))
						Head.AddAtomDiscovery(title, Globals.FullPath(bu.Atom(cntx.ApplicationKey)), Context);
					else if (cntx.SiteSettings.EnableSecureSyndication)
						Head.AddAtomDiscovery(title, Globals.FullPath(SiteUrls.FormatUrlWithUserNameToken(bu.Atom(cntx.ApplicationKey), cntx.User, false)), Context);
                }
            }
        }

        protected virtual void SetMetaTags(Weblog blog)
        {
            
        }
	}
}
